using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Windows.Forms;

namespace cs2335.CCT
{
    public interface ISprite
    {
        Bitmap Image { get; set; }
    }
    
    public class Sprite : IComponent, ISprite
    {

        protected Rectangle spriteRectangle = new Rectangle(0, 0, 0, 0);
        protected SolidBrush spriteBrush = new SolidBrush(Color.Crimson);
        protected Font font = new Font("SansSerif", 10);
        protected CCTForm owner;
        protected string text;
        protected bool hasFocus = false;
        protected bool hidden = false;
        protected string name;
        protected Bitmap image;
        protected String filename;


        #region Properties
        public Point Location
        {
            get { return spriteRectangle.Location; }
            set { spriteRectangle.Location = value; Invalidate(); }
        }

        public Rectangle Bounds
        {
            get { return spriteRectangle; }
            set { spriteRectangle = value; Invalidate(); }
        }

        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        public System.Drawing.Font Font
        {
            get { return font; }
            set { font = value; Invalidate(); }
        }

        public string Text
        {
            get { return text; }
            set { text = value; Invalidate(); }
        }

        public CCTForm Owner
        {
            get { return owner; }
            set { owner = value; }
        }

        public Color BackColor
        {
            get { return spriteBrush.Color; }
            set { spriteBrush.Color = value; Invalidate(); }
        }

        public Bitmap Image
        {
            get { return image; }
            set { 
                image = value;
                if (image!=null) spriteRectangle = new Rectangle(spriteRectangle.Left, spriteRectangle.Top, image.Width, image.Height);
                Invalidate(); }
        }

        #endregion

        #region Events
        public event EventHandler Click;
        public event EventHandler GotFocus;
        public event EventHandler KeyPress;
        public event EventHandler LostFocus;
        public event EventHandler Paint;
        public event EventHandler KeyDown;
        #endregion

        public Sprite(Bitmap b)
        {
            this.Image = b;
        }

        public Sprite(String filename)
        {
            this.Image = new Bitmap(System.Drawing.Image.FromFile(filename));
        }

        public void Invalidate()
        {
            if (owner != null)
            {
                owner.Invalidate();
            }
        }

        public void Hide()
        {
            hidden = true;
            Invalidate();
        }

        public void Show()
        {
            hidden = false;
            Invalidate();
        }


        public void OnPaint(PaintEventArgs e)
        {
            if (!hidden)
            {
                Graphics g = e.Graphics;
                g.DrawImage(image, spriteRectangle);
                if (Paint != null)
                {
                    Paint(this, e);
                }
            }

        }

        public void OnGotFocus(EventArgs e)
        {
            hasFocus = true;
            if (GotFocus != null)
            {
                GotFocus(this, e);
            }
            Invalidate();
        }

        public void OnLostFocus(EventArgs e)
        {
            hasFocus = false;
            if (LostFocus != null)
            {
                LostFocus(this, e);
            }
            Invalidate();
        }


        public void OnMouseClick(System.Windows.Forms.MouseEventArgs e)
        {
            if (Click != null)
            {
                Click(this, e);
            }
        }

        public void OnKeyPress(System.Windows.Forms.KeyPressEventArgs e)
        {
            //Invokes a Click event if the user hits ENTER
            //  while this component has focus
            if (KeyPress != null)
            {
                KeyPress(this, e);
            }
        }

        public void OnKeyDown(KeyEventArgs e)
        {
            if (KeyDown != null)
            {
                KeyDown(this, e);
            }
        }

    }
}
